//
//  RHPlantListViewCell.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/7/24.
//

#import "RHPlantListViewCell.h"
#import "RHTools.h"


@interface RHPlantListViewCell()

@property (nonatomic ,strong) UIImageView *plantPic;

@property (nonatomic ,strong) QMUIButton *stateBtn;

@property (nonatomic ,strong) QMUILabel *plantName;

@property (nonatomic ,strong) QMUILabel *typeLab;

@property (nonatomic ,strong) QMUILabel *maintainerLab;

@property (nonatomic ,strong) QMUILabel *addressLab;

@property (nonatomic ,strong) QMUIButton *deleteBtn;

@property (nonatomic, strong) UIView *pointView;

@property (nonatomic ,strong) QMUILabel *statusLab;

@end

@implementation RHPlantListViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setModel:(RHPlantModel *)model
{
    _model = model;
    self.plantName.text = model.plantName;
   
    [self.plantPic sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"plant_default"]];
   
   
    
    if([_model.status isEqualToString:@"1"]){
        self.pointView.backgroundColor = kCOLOR_HEX(@"BBBBBB");
        self.statusLab.text = @"离线".rh_localized;
    }else if([_model.status isEqualToString:@"0"]){
        self.pointView.backgroundColor = CMainColorGreen;
        self.statusLab.text = @"正常运行".rh_localized;
    }else if([_model.status isEqualToString:@"2"]){
        self.pointView.backgroundColor = kCOLOR_HEX(@"FA6334");
        self.statusLab.text = @"运行故障".rh_localized;
    }else if([_model.status isEqualToString:@"3"]){
        self.pointView.backgroundColor = kCOLOR_HEX(@"F5AF46");
        self.statusLab.text = @"运行告警".rh_localized;
    }
    self.typeLab.text = [NSString stringWithFormat:@"%@ - %@",ValidNullData(model.productTypeName),model.plantType];
    self.addressLab.text = ValidNullDataToString(model.address, @"--");
    self.maintainerLab.text = ValidNullDataToString(model.maintenance, @"--");
}

- (void)setupUI{

    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:UIColor.clearColor];
    
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:UIColor.whiteColor];
    [bgView.layer setCornerRadius:8];
    [self.contentView addSubview:bgView];
    
    _plantName = [[QMUILabel alloc] init];
    [_plantName setFont:[UIFont boldSystemFontOfSize:16]];
    _plantName.adjustsFontSizeToFitWidth = YES;
    _plantName.minimumScaleFactor = 0.8;
    [_plantName setTextColor:kCOLOR_HEX(@"3F4443")];
    [bgView addSubview:_plantName];
    
    QMUILabel *seeLab = [QMUILabel labelWithText:@"查看详情".rh_localized font:kSYSTEMFONT(12) textColor:kCOLOR_HEX_A(@"132B88",0.6)];
    [bgView addSubview:seeLab];
    
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"kwt_home_arrow"]];
    [bgView addSubview:arrowImg];
    
    _plantPic = [[UIImageView alloc] init];
    _plantPic.contentMode = UIViewContentModeScaleAspectFill;
    [_plantPic setBackgroundColor:UIColor.lightGrayColor];
    [_plantPic.layer setCornerRadius:8];
    _plantPic.layer.masksToBounds = YES;
    [bgView addSubview:_plantPic];
    
   
    
    _typeLab = [[QMUILabel alloc] init];
    [_typeLab setFont:[UIFont systemFontOfSize:14]];
    [_typeLab setTextColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8]];
    [bgView addSubview:_typeLab];
    
    
   
    
    QMUIButton *maintainerBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [maintainerBtn setImagePosition:QMUIButtonImagePositionLeft];
    [maintainerBtn setImage:[UIImage imageNamed:@"kwt_plant_maintainer"] forState:UIControlStateNormal];
    [maintainerBtn setTitle:[NSString stringWithFormat:@"%@:",@"维护人员".rh_localized] forState:UIControlStateNormal];
    [maintainerBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [maintainerBtn setTitleColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
    [maintainerBtn setSpacingBetweenImageAndTitle:5];
    [maintainerBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [bgView addSubview:maintainerBtn];
    
    _maintainerLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#171A1D") textAligment:NSTextAlignmentRight];
    _maintainerLab.text = @"--";
    [bgView addSubview:_maintainerLab];
    
    QMUIButton *addressBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [addressBtn setImagePosition:QMUIButtonImagePositionLeft];
    [addressBtn setImage:[UIImage imageNamed:@"kwt_plant_address"] forState:UIControlStateNormal];
    [addressBtn setTitle:[NSString stringWithFormat:@"%@:",@"电站地址".rh_localized] forState:UIControlStateNormal];
    [addressBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [addressBtn setTitleColor:[kCOLOR_HEX(@"3F4443") colorWithAlphaComponent:0.8] forState:UIControlStateNormal];
    [addressBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [addressBtn setSpacingBetweenImageAndTitle:5];
    [bgView addSubview:addressBtn];

    _addressLab = [QMUILabel labelWithFont:kSYSTEMFONT(12) textColor:kCOLOR_HEX(@"#171A1D") textAligment:NSTextAlignmentRight];
    _addressLab.text = @"--";
    _addressLab.numberOfLines = 2;
    [bgView addSubview:_addressLab];
    
    kWeakSelf;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];

    [_plantName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_offset(15);
        make.right.equalTo(seeLab.mas_right).offset(-5);
        make.height.mas_equalTo(25);
    }];
    
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(15);
        make.centerY.equalTo(weakSelf.plantName);
        make.right.mas_offset(-15);
    }];
    
    [seeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.plantName);
        make.right.equalTo(arrowImg.mas_left).offset(-3);
    }];
    
    [_plantPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(weakSelf.plantName.mas_bottom).offset(15);
        make.height.offset(kScaleW(130));
    }];
    
    [maintainerBtn.titleLabel sizeToFit];
    [maintainerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.plantName);
        make.top.equalTo(weakSelf.plantPic.mas_bottom).offset(15);
        make.height.mas_equalTo(20);
        make.width.offset(maintainerBtn.titleLabel.width + 30);
    }];
    
    [_maintainerLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(maintainerBtn.mas_right).offset(5);
        make.right.mas_offset(-15);
        make.centerY.equalTo(maintainerBtn);
    }];
    
    [addressBtn.titleLabel sizeToFit];
    [addressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.plantName);
        make.top.equalTo(maintainerBtn.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
        make.width.offset(addressBtn.titleLabel.width + 30);
    }];
    
    [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(addressBtn.mas_right).offset(5);
        make.right.mas_offset(-15);
        make.top.equalTo(addressBtn).offset(3);
        make.bottom.mas_offset(-15);
    }];
    
//    [_typeLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.equalTo(weakSelf.plantName);
//        make.top.equalTo(weakSelf.plantName.mas_bottom).offset(5);
//        make.height.mas_equalTo(20);
//    }];
    
    
    
    [[_deleteBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x) {
        if(weakSelf.deleteBlock){
            weakSelf.deleteBlock(weakSelf.idx);
        }
    }];
    

    UIView *statusView = [[UIView alloc]init];
    [_plantPic addSubview:statusView];
    statusView.layer.cornerRadius = 8;
    statusView.layer.masksToBounds = YES;
    statusView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.3];
    
    UIView *pointView = [[UIView alloc]init];
    [statusView addSubview:pointView];
    pointView.layer.cornerRadius = 2.5;
    pointView.layer.masksToBounds = YES;
    self.pointView = pointView;
    
    QMUILabel *statusLab = [[QMUILabel alloc]init];
    [statusView addSubview:statusLab];
    statusLab.font = [UIFont systemFontOfSize:12];
    statusLab.textColor = [UIColor whiteColor];
    statusLab.textAlignment = NSTextAlignmentCenter;
    self.statusLab = statusLab;
    
    [statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.offset(25);
    }];
    
    [statusLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(5);
        make.bottom.mas_offset(-5);
        make.right.mas_offset(-10);
    }];
    [pointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.centerY.mas_offset(0);
        make.right.equalTo(statusLab.mas_left).offset(-5);
        make.width.height.offset(5);
    }];
    
    
    
    
    [_plantPic setImage:[UIImage imageNamed:@"mask_group"]];
   
    
}
@end
